/*
 CAM SPECTATE SCRIPT 

 Upon the player falling dead creates a camera that follows the nearest alive friendly unit

 JULY 2009 - norrin
 
**************************************************************************************************************************
 Start camera_spectate.sqf
*/

disableserialization;

//define variables
_Object 					= _this select 0;
_pos 						= _this select 1;

_c 							= 0; 
_d 							= 0;
_allUnits 					= [];
_alive_friends 				= [];

NORRN_FOCUS_CAM_ON 			= 0;
NORRN_cam_TYPE 				= 0;
NORRN_CAM_NVG 				= false;
NORRN_CAM_OFF				= false;

_top_view_height 			= 50;
_r_dialog_0 				= "";
_r_display  				= "";

// Variables for attachTo camera
_oldPos 			= [0,0,0];
_freePos 			= [0,-5,2];
_oldFocus 			= objNull;
_oldCam 			= -1; 
_r_marker_side1		= "";
_r_marker_side2		= "";

titletext ["Camera Initialising","BLACK FADED",3];

//set camera to target body
_camx = _pos select 0;
_camy = _pos select 1;
_camz = _pos select 2;

NORRN_cam = "camera" CamCreate [_camx,_camy+5,_camz+1];
NORRN_cam CamSetTarget player;
NORRN_cam CameraEffect ["INTERNAL","Back"];
NORRN_cam CamCommit 2;

//setVars for free camera 
_angh = 0; 
_angv = 45;
sleep 0.1;

_allUnits = allUnits;
{if (side _x != sideLogic && alive _x ) then {_alive_friends = _alive_friends + [_x]}} forEach _allUnits;
COUNT_CAM_friends = count _alive_friends;

//create dialog and initialise keyboard;
_r_dialog_0 = createDialog "NORRN_cam_dialog"; 
_r_display = findDisplay 99130;
_r_display displaySetEventHandler ["Keydown", "_this call CAM_KEY_pressed"];
titletext [" ","BLACK IN",1];
CAM_KEY_pressed = compile preprocessfile "cScripts\_cameraApps\NORRN_cam\CAM_KEY_pressed.sqf";

//change camera via dialog
lbClear 10004;
_index = lbAdd[10004, "3rd Person"];
_index = lbAdd[10004, "Top Down"];
_index = lbAdd[10004, "Front Side"];
_index = lbAdd[10004, "1st Person"];
_index = lbAdd[10004, "Follow/Free"];
_index = lbAdd[10004, "Static"];
lbSetCurSel [10004, 0];

lbClear 10005;
{_index_friends = lbAdd[10005, name _x]; sleep 0.01} forEach allUnits;
lbSetCurSel [10005, lbCurSel 10005];

while {!NORRN_CAM_OFF} do 
{
	scopeName "FollowCam_01";
		
	//fix for changing dialogs
	_cam_angle = lbCurSel 10004;
	_cam_focus = lbCurSel 10005;
	
	// =================================================================================================================================
	if (_c == 500) then 
	{
		//change target via dialog
		lbClear 10005;
		{_index_friends = lbAdd[10005, name _x]} forEach allUnits;
		lbSetCurSel [10005, lbCurSel 10005];
	};
	_c = _c + 1;

	NORRN_cam_TYPE = lbCurSel 10004;
	NORRN_FOCUS_CAM_ON = lbCurSel 10005;
	
	_unit = _alive_friends select NORRN_FOCUS_CAM_ON; 
	_target = vehicle _unit;
	
	if (!dialog) then {if (!isNull _me) then {_target = _me; NORRN_cam camsettarget _target;NORRN_cam cameraeffect ["internal", "back"];NORRN_cam camsetrelpos [-3, +1, (_max_box select 2) +1];NORRN_cam camcommit 1};};
	
	if (NORRN_cam_TYPE == 0 || NORRN_cam_TYPE == 3) then {_oldFocus = objNull; detach NORRN_cam;}; 
	if (NORRN_cam_TYPE == 0) then {_target switchCamera "EXTERNAL"; NORRN_cam CameraEffect ["Terminate","Back"];};
	if (NORRN_cam_TYPE == 1) then 
	{	
		showcinemaborder false;
		NORRN_cam camsettarget _target; 
		NORRN_cam cameraeffect ["internal", "back"];
		NORRN_cam attachTo [_target,[0, -2, OFPEC_range_to_unit]];
		NORRN_cam camcommit 0.01;
		_oldFocus = _target;	
	};
	if (NORRN_cam_TYPE == 2) then 
	{	
		if (_target != _oldFocus || _oldcam != NORRN_cam_TYPE) then
		{
			NORRN_cam camsettarget _target; 
			NORRN_cam cameraeffect ["internal", "back"];
			NORRN_cam attachTo [_target,[-1.5, 3, 0.2]];
			NORRN_cam camSetFov 1.1; 
			NORRN_cam camcommit 0.01;
			_oldFocus = _target;
		};
	};
	if (NORRN_cam_TYPE == 3) then {_target switchCamera "INTERNAL";NORRN_cam CameraEffect ["Terminate","Back"]};
	if (NORRN_cam_TYPE == 4) then
	{ 	 
		// Mouse controls - Free cam courtesy of hoz and mandoble see: OFPEC - see: http://www.ofpec.com/forum/index.php?topic=32970.0
		if (OFPEC_MouseButtons select 1) then 
		{
			if (((OFPEC_MouseCoord select 0) >= 0) && ((OFPEC_MouseCoord select 0) <= 1) &&
			((OFPEC_MouseCoord select 1) >= 0) && ((OFPEC_MouseCoord select 1) <= 1)) then
			{
				_deltah = (0.5 - (OFPEC_MouseCoord select 0))*10/0.2;
				_deltav = (0.5 - (OFPEC_MouseCoord select 1))*10/0.2;
				_angv = (_angv + _deltav);
				_angh = (_angh + _deltah);
				_angv = _angv max 0;			
				_angv = _angv min 89;
			};						
		};
		
		// Change target if new 
		if (_target != _oldFocus || _oldcam != NORRN_cam_TYPE) then
		{	
			NORRN_cam CamSetTarget _target;
			NORRN_cam CameraEffect ["INTERNAL","Back"];
			NORRN_cam CamCommit 0.01;
			NORRN_cam attachTo [_target,_freePos];
			_oldFocus = _target;
		};
		
		_freePos = [sin(_angh)*OFPEC_range_to_unit, cos(_angh)*OFPEC_range_to_unit, OFPEC_range_to_unit*sin(_angv)];
		
		// Attach Camera
		if ((_freePos select 0) != (_oldPos select 0) || (_freePos select 1) != (_oldPos select 1) || (_freePos select 2) != (_oldPos select 2)) then
		{
			//detach NORRN_cam;
			NORRN_cam attachTo [_target,_freePos];
			_oldPos = _freePos; 
		};
	};
	
	if (NORRN_cam_TYPE == 5) then
	{	
		if (_target != _oldFocus || _oldcam != NORRN_cam_TYPE) then 
		{
			detach NORRN_cam;
			_d = 1; 
			_static_cam_pos = [(getPosASL _target) select 0, ((getPosASL _target) select 1) + 2, ((getPosASL _target) select 2) + 2];
			NORRN_cam camsettarget _target; 
			NORRN_cam cameraeffect ["internal", "back"];
			//NORRN_cam camSetPos _static_cam_pos;
			NORRN_cam camCommit 0.01;
			_oldFocus = _target;
		};
	};
	//========================================================================================	
	_oldCam = NORRN_cam_TYPE;
	sleep 0.02;
};

//destroy camera
if (NORRN_CAM_OFF) exitWith 
{
	camUseNVG false;
	closeDialog 0;
	vehicle player switchCamera "INTERNAL"; 
	NORRN_cam CameraEffect ["Terminate","Back"];
	sleep 0.1;
	NORRN_REVIVE_cam CameraEffect ["Terminate","Back"];
	CamDestroy NORRN_cam;
	deleteVehicle _Object;
};

//Last edited 18/06/09